
Partial Class NewsDetails
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'MsgBox(Session("type"), , "type")
        'MsgBox(Session("page"), , "page")
        'MsgBox(Session("clicked"), , "clicked")
        If Session("type") = "" Or (Session("type") <> "admin" And Session("page") <> "NR") Then
            Response.Redirect("Error.aspx")
            Exit Sub
        End If

        lnkProfName.Text = "Logged in as " & Session("prim") & " (" & Session("sec") & ")"
        'MsgBox("prim --> current user: " & Session("prim"), , "prim")



        If Not Me.IsPostBack = True Then 'Or Session("Go") = True Then
            If Session("type") = "admin" Then
                lnkDel.Visible = True
                If Session("ViewMore") = True Then lnkDel.Enabled = True
                lblPid.Visible = True
                'ddlPid.Visible = True

                Dim a As New Globo
                a.executesql("select * from posts order by pid desc", False)
                ddlPid.Items.Clear()
                ddlPid.Items.Add("")
                While a.dr.Read = True
                    ddlPid.Items.Add(a.dr(0))
                    'MsgBox("pid [a.dr(0)] = " & a.dr(0), , "pid")
                End While
                'ddlPid.Text = "select a post_id from the list"

                If Session("page") <> "NR" Then
                    Exit Sub
                End If
            Else 'If Session("type") = "user" Or Session("type") = "buyer" Then
                Dim a As New Globo
                a.executesql("select * from posts order by pid desc", False)
                ddlPid.Items.Clear()
                While a.dr.Read = True
                    ddlPid.Items.Add(a.dr(8))
                    'MsgBox("header [a.dr(8)] = " & a.dr(8), , "header")
                End While
                'ddlPid.Text = "select a post_id from the list"
            End If


            Dim pid, uid As String
            pid = Session("pid") 'lblPid.Text
            uid = Session("prim")
            'MsgBox("pid : " & pid, , "pid")
            If pid <> "[pid]" And pid <> "" Then
                Dim c As New Globo
                Dim cd As Boolean
                c.executesql("select * from votes where pid='" & pid & "' and uid='" & uid & "'", False)
                cd = c.dr.Read
                'MsgBox("c.dr.Read = " & c.dr.Read, , "read")
                'MsgBox("cd = " & cd, , "cd")
                If cd = False Then
                    imgThumbsUp.Enabled = True
                    imgThumbsDown.Enabled = True
                    Session("clicked") = False
                Else
                    imgThumbsUp.Enabled = False
                    imgThumbsDown.Enabled = False
                    Session("clicked") = True
                End If
                lnkDel.Enabled = True
            End If


            If Session("page") = "NR" Then 'And Session("ViewMore") = True Then
                'ddlPid.Text = Session("pid")

                Dim str As String
                str = Session("pid")

                Dim b As New Globo
                b.executesql("select pid, uid, header, body, ntype, country, reg, subreg, dt_post, time_post, anon, buy_status, thumbsup, thumbsdown from posts where pid='" & str & "'", False)

                If b.dr.Read = True Then
                    Dim strposttime As String
                    strposttime = b.dr(8).ToString & " " & b.dr(9).ToString
                    lblPid.Text = b.dr(0)
                    lblTitle.Text = b.dr(2)
                    lblBody.Text = b.dr(3)
                    lblNType.Text = b.dr(4)
                    lnkCon.Text = b.dr(5)
                    lblReg.Text = b.dr(7) & ", " & b.dr(6)
                    lblVoteUp.Text = b.dr(12)
                    lblVoteDown.Text = b.dr(13)

                    If Session("type") = "user" And b.dr(1) = Session("prim") Then lnkDel.Visible = True
                    If b.dr(10) = False Then
                        lblPostAt.Text = "Posted by '" & b.dr(1) & "' on " & strposttime
                        If b.dr(1) = Session("prim") Then lblPostAt.Text = "Posted by you on " & strposttime
                    ElseIf b.dr(10) = True Then
                        lblPostAt.Text = "Posted anonymously on " & strposttime
                        If Session("type") = "admin" Then lblPostAt.Text = "Posted by '" & b.dr(1) & "' [anonymously] on " & strposttime
                        If Session("type") = "user" And b.dr(1) = Session("prim") Then lblPostAt.Text = "Posted by you [anonymously] on " & strposttime
                    End If

                    If b.dr(11) = "Available" Then
                        lblStatus.Text = "News available for sale. Bid now!"
                        lblStatus.ForeColor = Drawing.Color.Green
                    ElseIf b.dr(11) = "Sold" Then
                        lblStatus.Text = "News already sold!"
                        lblStatus.ForeColor = Drawing.Color.Brown
                    End If
                Else
                    lblErr.Text = "No deatils found!"
                    lblErr.Visible = True
                End If

                'MsgBox("imgThumbsUp.Enabled = " & imgThumbsUp.Enabled, , "UP")
                'MsgBox("imgThumbsDown.Enabled = " & imgThumbsDown.Enabled, , "DOWN")
            Else
                Response.Redirect("Error.aspx")
                Exit Sub
            End If
        End If
    End Sub

    Protected Sub imgThumbsDown_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgThumbsDown.Click
        'lblVoteDown.Text = Val(lblVoteDown.Text) + 1

        Dim prim, type As String
        prim = Session("prim")
        type = Session("type")
        'MsgBox("prim --> current user: " & prim, , "prim")
        'MsgBox(imgThumbsUp.Enabled, , "Up-en")
        'MsgBox(imgThumbsDown.Enabled, , "Dn-en")
        'MsgBox(Session("clicked"), , "clicked")
        If imgThumbsUp.Enabled = True And imgThumbsDown.Enabled = True And Session("clicked") = False Then
            lblVoteDown.Text = Val(lblVoteDown.Text) + 1

            Dim a As New Globo
            a.executesql("insert into votes values ('" & lblPid.Text & "', '" & prim & "', '" & type & "', 'DOWN')", True)

            Dim b As New Globo
            b.executesql("update posts set thumbsup='" & lblVoteUp.Text & "', thumbsdown='" & lblVoteDown.Text & "' where pid='" & lblPid.Text & "'", True)

            Session("clicked") = True
        End If

        imgThumbsDown.Enabled = False
        imgThumbsUp.Enabled = False
        'MsgBox(lblVoteUp.Text & ", " & lblVoteDown.Text, MsgBoxStyle.Information, "Up-Dn")
    End Sub

    Protected Sub imgThumbsUp_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgThumbsUp.Click
        'lblVoteUp.Text = Val(lblVoteUp.Text) + 1

        Dim prim, type As String
        prim = Session("prim")
        type = Session("type")
        'MsgBox("prim --> current user: " & prim, , "prim")
        'MsgBox(imgThumbsUp.Enabled, , "Up-en")
        'MsgBox(imgThumbsDown.Enabled, , "Dn-en")
        'MsgBox(Session("clicked"), , "clicked")
        If imgThumbsUp.Enabled = True And imgThumbsDown.Enabled = True And Session("clicked") = False Then
            lblVoteUp.Text = Val(lblVoteUp.Text) + 1

            Dim a As New Globo
            a.executesql("insert into votes values ('" & lblPid.Text & "', '" & prim & "', '" & type & "', '" & "UP" & "')", True)

            Dim b As New Globo
            b.executesql("update posts set thumbsup='" & lblVoteUp.Text & "', thumbsdown='" & lblVoteDown.Text & "' where pid='" & lblPid.Text & "'", True)

            Session("clicked") = True
        End If

        imgThumbsDown.Enabled = False
        imgThumbsUp.Enabled = False
        'MsgBox(lblVoteUp.Text & ", " & lblVoteDown.Text, MsgBoxStyle.Information, "Up-Dn")
    End Sub

    Protected Sub btnBid_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnBid.Click
        Response.Redirect("NewsBid.aspx")
    End Sub

    Protected Sub lnkDel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDel.Click
        pnlNewsDet.Enabled = False
        pnlDel.Visible = True
    End Sub

    Protected Sub btnY_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnY.Click
        Dim a As New Globo
        a.executesql("delete from posts where pid='" & lblPid.Text & "'", True)

        Dim b As New Globo
        b.executesql("delete from nimage where pid='" & lblPid.Text & "'", True)

        Dim c As New Globo
        c.executesql("delete from nvid where pid='" & lblPid.Text & "'", True)

        Session("NRload") = 0
        Response.Redirect("NewsRoom.aspx")
    End Sub

    Protected Sub btnN_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnN.Click
        pnlDel.Visible = False
        pnlNewsDet.Enabled = True
    End Sub

    Protected Sub ddlPid_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPid.SelectedIndexChanged
        Session("pid") = ddlPid.Text
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If ddlPid.Text = "" Then Exit Sub

        'Dim str As String
        'str = Session("pid") 'Session("pid_new")
        If Session("type") = "admin" Then
            Session("pid") = ddlPid.Text
        Else 'If Session("type") = "buyer" Or Session("type") = "admin" Then
            Dim a As New Globo
            a.executesql("select * from posts where header ='" & ddlPid.Text & "'", False)
            While a.dr.Read
                Session("pid") = a.dr(0)
            End While
        End If

        Session("page") = "NR"
        'MsgBox("Session(""pid"") = " & Session("pid"), , "pid")
        'MsgBox("Session(""page"") = " & Session("page"), , "page")
        Session("Go") = True

        Call Page_Load(sender, e)        'Response.Redirect("NewsDetails.aspx")
    End Sub

    Protected Sub lnkNR_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkNR.Click
        Response.Redirect("NewsRoom.aspx")
        Session("NRlaod") = 0
    End Sub

    Protected Sub imgbtnNR_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnNR.Click
        Response.Redirect("NewsRoom.aspx")
        Session("NRlaod") = 0
    End Sub

    '_________________________________________________________________________________________________________________________________
    '#################################################################################################################################
    Protected Sub lnkProfName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkProfName.Click
        If Session("type") <> "" Then
            Dim str As String
            str = UCase(Left(Session("type"), 1))
            Response.Redirect("" & str & "Profile.aspx")
        End If
    End Sub

    'Protected Sub lnkQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkQuery.Click
    '    Response.Redirect("Query.aspx")
    'End Sub

    Protected Sub lnkThemes_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkThemes.Click
        Response.Redirect("Themes.aspx")
    End Sub

    Protected Sub lnkSettings_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSettings.Click
        Response.Redirect("Settings.aspx")
    End Sub

    Protected Sub lnkLogOut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLogOut.Click
        Server.Transfer("Loading.aspx")
    End Sub

    '_____________________________________________________________________________________________________________
    '*************************************************************************************************************
    Protected Sub lnkAboutUs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkAboutUs.Click
        Response.Redirect("AboutUs.aspx")
    End Sub

    Protected Sub lnkTermCond_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkTermCond.Click
        Response.Redirect("TermsConditions.aspx")
    End Sub

    Protected Sub lnkContactUs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkContactUs.Click
        Response.Redirect("ContactUs.aspx")
    End Sub

    Protected Sub lnkFAQs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFAQs.Click
        Response.Redirect("FAQs.aspx")
    End Sub

    Protected Sub lnkFeedback_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFeedback.Click
        Response.Redirect("Feedback.aspx")
    End Sub
End Class